package Visual;

import Logica.Personal;
import Logica.Plantacion;
import Logica.Quimico;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class frmAltaAplicacion extends javax.swing.JDialog {
    private Principal unaPrincipal = new Principal();
    private ControladoraVisual miControladora = new ControladoraVisual();
    private DefaultListModel modeloPlantaciones = new DefaultListModel();
    private DefaultListModel modeloPersonal = new DefaultListModel();
    private DefaultListModel modeloQuimicos = new DefaultListModel();
    
    public frmAltaAplicacion(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);
        
        setTitle("Alta Aplicación");
        initComponents();
        this.cargarListaPlantaciones();
        this.cargarListaPersonal();
        this.cargarListaQuimicos();
        this.lstPersonal.setModel(modeloPersonal);
        this.lstPlantacion.setModel(modeloPlantaciones);
        this.lstQuimicos.setModel(modeloQuimicos);
        this.setLocationRelativeTo(unaPrincipal);
        
    }
    
    private void cargarListaPlantaciones(){
        List<Plantacion> plantaciones = miControladora.cargarListaPlantaciones();
        for (Plantacion unaPlantacion:plantaciones)
            this.modeloPlantaciones.addElement(unaPlantacion);
    }
    
    private void cargarListaPersonal(){
        Map<String,Personal> personal = miControladora.cargarListaPersonal();
        Iterator it = personal.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry aux = (Map.Entry) it.next();
            Personal unPersonal = (Personal) aux.getValue();
            personal.put(unPersonal.getDni(),unPersonal);
        }
    }
    private void cargarListaQuimicos(){
        List<Quimico> quimicos = miControladora.cargarListaQuimicos();
        for (Quimico unQuimico:quimicos)
            this.modeloQuimicos.addElement(unQuimico);
        
    }
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFecha = new javax.swing.JLabel();
        btnAlta = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstPlantacion = new javax.swing.JList();
        lblPlantacion = new javax.swing.JLabel();
        lblPersonal = new javax.swing.JLabel();
        lblQuimico = new javax.swing.JLabel();
        JScrollPane = new javax.swing.JScrollPane();
        lstQuimicos = new javax.swing.JList();
        JScrollPane3 = new javax.swing.JScrollPane();
        lstPersonal = new javax.swing.JList();
        btnAltaPlantacion = new javax.swing.JButton();
        btnAgregarPersonal = new javax.swing.JButton();
        btnAgregarQuimico = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        cmbDia = new javax.swing.JComboBox();
        cmbMes = new javax.swing.JComboBox();
        cmbAnio = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblFecha.setText("Fecha");

        btnAlta.setText("Alta");
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        lstPlantacion.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "No hay plantaciones." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstPlantacion);

        lblPlantacion.setText("Plantación");

        lblPersonal.setText("Personal");

        lblQuimico.setText("Quimico");

        lstQuimicos.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "No hay químicos." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        JScrollPane.setViewportView(lstQuimicos);

        lstPersonal.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "No hay personal." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        JScrollPane3.setViewportView(lstPersonal);

        btnAltaPlantacion.setText("Agregar Plantación");
        btnAltaPlantacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaPlantacionActionPerformed(evt);
            }
        });

        btnAgregarPersonal.setText("Agregar Personal");
        btnAgregarPersonal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarPersonalActionPerformed(evt);
            }
        });

        btnAgregarQuimico.setText("Agregar Químico");
        btnAgregarQuimico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarQuimicoActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        cmbDia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        cmbDia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbDiaActionPerformed(evt);
            }
        });

        cmbMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        cmbAnio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2013", "2014", "2015", "2016" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAlta)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblFecha)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane1)
                                .addComponent(btnAltaPlantacion, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                            .addComponent(lblPlantacion))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(lblPersonal)
                                .addGap(75, 75, 75)
                                .addComponent(lblQuimico)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnAgregarPersonal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(JScrollPane3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(JScrollPane)
                                    .addComponent(btnAgregarQuimico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 103, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmbDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(cmbMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFecha)
                    .addComponent(cmbDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPlantacion)
                    .addComponent(lblPersonal)
                    .addComponent(lblQuimico))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAltaPlantacion)
                    .addComponent(btnAgregarPersonal)
                    .addComponent(btnAgregarQuimico))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAlta)
                    .addComponent(btnCancelar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbDiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbDiaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbDiaActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAltaPlantacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaPlantacionActionPerformed
        frmAltaPlantacion unaVentana = new frmAltaPlantacion(unaPrincipal,true,false,this);
        unaVentana.setVisible(true);
    }//GEN-LAST:event_btnAltaPlantacionActionPerformed

    private void btnAgregarPersonalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarPersonalActionPerformed
        frmAltaPersonal unaVentana = new frmAltaPersonal(unaPrincipal,true,false,this);
        unaVentana.setVisible(true);
    }//GEN-LAST:event_btnAgregarPersonalActionPerformed

    private void btnAgregarQuimicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarQuimicoActionPerformed
        frmAltaQuimico unaVentana = new frmAltaQuimico(unaPrincipal,true,false,this);
        unaVentana.setVisible(true);
    }//GEN-LAST:event_btnAgregarQuimicoActionPerformed

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        this.altaAplicacion();
        
    }//GEN-LAST:event_btnAltaActionPerformed

    private void altaAplicacion(){
        Date fecha=null;
        String dia = (String) this.cmbDia.getSelectedItem();
        String mes = (String) this.cmbMes.getSelectedItem();
        String anio = (String) this.cmbAnio.getSelectedItem();
        try{
            String laFecha = dia + "/" + mes + "/" + anio;
            SimpleDateFormat formatoDeFecha = new SimpleDateFormat("dd/MM/yyyy");
            fecha = formatoDeFecha.parse(laFecha);
        }
        catch(ParseException a){
            JOptionPane.showMessageDialog(this, a.getMessage(), "Lo sentimos, algo salió mal", JOptionPane.WARNING_MESSAGE);
        }
        Plantacion unaPlantacion = (Plantacion) this.lstPlantacion.getSelectedValue();
        List<Personal> personal = (List<Personal>) this.lstPersonal.getSelectedValuesList();
        Quimico unQuimico = (Quimico) this.lstQuimicos.getSelectedValue();
        miControladora.altaAplicacion(fecha,unaPlantacion,personal,unQuimico);
    }
    
    
    private void conseguirValores(){
        
    }
    
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    public void mainMetodo(){
       try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmAltaAplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmAltaAplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmAltaAplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmAltaAplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
                 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmAltaAplicacion dialog = new frmAltaAplicacion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane JScrollPane;
    private javax.swing.JScrollPane JScrollPane3;
    private javax.swing.JButton btnAgregarPersonal;
    private javax.swing.JButton btnAgregarQuimico;
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnAltaPlantacion;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbAnio;
    private javax.swing.JComboBox cmbDia;
    private javax.swing.JComboBox cmbMes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblPersonal;
    private javax.swing.JLabel lblPlantacion;
    private javax.swing.JLabel lblQuimico;
    private javax.swing.JList lstPersonal;
    private javax.swing.JList lstPlantacion;
    private javax.swing.JList lstQuimicos;
    // End of variables declaration//GEN-END:variables
void btnAgregarPlantacion(Plantacion unaPlantacion) {
        modeloPlantaciones.addElement(unaPlantacion); //To change body of generated methods, choose Tools | Templates.
    }

public void obtenerPersonal(Personal persona) {
        modeloPersonal.addElement(persona); //To change body of generated methods, choose Tools | Templates.
    }

    void agregarQuimico(Quimico unQuimico) {
        modeloQuimicos.addElement(unQuimico); //To change body of generated methods, choose Tools | Templates.
    }

}
